package com.lipuwater.agent.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2016/12/8.
 * Email:924686754@qq.com
 * 左右滑动标签栏
 */
public class TabLayoutView extends View {
    /**
     * 画字
     */
    private Paint mTextPaint;
    private Paint mLinePaint;

    private int mTextDefaultColor = 0xff909090;
    private int mTextSelectColor = 0xff228efd;
    private int mLineColor = 0xff228efd;

    private int marginLeft = dip2px(20);
    private int marginRight = dip2px(20);

    private int mTextSize = dip2px(15);

    private String[] tabs = {"---"};

    private int mViewWidth;
    private int mViewHeight;

    //保存每个tab的X开始坐标位置和结束坐标
    private List<Float> tabsXStart = new ArrayList<>();
    private List<Float> tabsXEnd = new ArrayList<>();
    //当前tab位置
    private int currentTab = 0;
    private OnTabSelectListener mOnTabSelectListener;

  /*  public TabLayoutView(Context context) {
        this(context, null);
        initPaint();
    }*/

    public TabLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }


    private void initPaint() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setStrokeWidth(dip2px(0.1f));
        mTextPaint.setStrokeCap(Paint.Cap.ROUND);
        mTextPaint.setTextSize(mTextSize);
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setStrokeWidth(dip2px(0.1f));
        mLinePaint.setStrokeCap(Paint.Cap.ROUND);
        mLinePaint.setColor(mLineColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            drawText(canvas);
            drawLine(canvas);
        } catch (Exception e) {
        }
    }


    /**
     * 绘制文字并且保存每个tab的开始坐标和结束坐标
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        mTextPaint.setColor(mTextDefaultColor);
        int currentDrawX = 0;//绘制到当前X轴坐标
        tabsXStart.add((float) 0);
        for (int i = 0; i < tabs.length; i++) {
            int StringWidth = i == 0 ? 0 : StringWidth(tabs[i - 1], mTextPaint);
            currentDrawX += marginLeft + StringWidth;
            if (tabsXEnd.size() > 0) {
                tabsXStart.add(tabsXEnd.get(i - 1) - marginLeft);
            }
            tabsXEnd.add((float) currentDrawX + StringWidth(tabs[i], mTextPaint) + marginLeft);
            float drawY = (float) (getMeasuredHeight() / 2 + getTextHeight(mTextPaint) / 4);
            if (currentTab == i) {
                mTextPaint.setColor(mTextSelectColor);
            } else {
                mTextPaint.setColor(mTextDefaultColor);
            }
            canvas.drawText(tabs[i], currentDrawX, drawY, mTextPaint);
        }
    }

    /**
     * 绘制下划线
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        canvas.drawRect(tabsXStart.get(currentTab) + dip2px(10), getMeasuredHeight() - dip2px(2), tabsXEnd.get(currentTab) - dip2px(10), getMeasuredHeight(), mLinePaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                float x = event.getX();
                for (int i = 0; i < tabsXStart.size(); i++) {
                    if ((x > tabsXStart.get(i) && (x < tabsXEnd.get(i)))) {
                        if (mOnTabSelectListener != null) {
                            mOnTabSelectListener.onTabSelect(i, (tabsXStart.get(i) + tabsXEnd.get(i)) / 2);
                        }
                        setSelectTab(i);
                        break;
                    }
                }
                break;
        }
        return true;
    }

    public void setTabs(String... tabs) {
        this.tabs = tabs;
    }

    /**
     * 切换到当前view
     */
    private void setSelectTab(int tab) {
        currentTab = tab;
        tabsXStart.clear();
        tabsXEnd.clear();
        invalidate();
    }

    public void setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.mOnTabSelectListener = onTabSelectListener;
    }

    public interface OnTabSelectListener {
        void onTabSelect(int position, float lineMiddleX);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mViewWidth = w;
        this.mViewHeight = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 0;
        for (int i = 0; i < tabs.length; i++) {
            width += marginLeft + StringWidth(tabs[i], mTextPaint);
        }
        width += marginRight;
        setMeasuredDimension(width, heightMeasureSpec);
    }


    /**
     * 计算出文字占多宽的宽度，renturn px 像素
     *
     * @param strings
     * @param p
     * @return
     */
    private int StringWidth(String strings, Paint p) {
        float maxWidthF = 0.0f;
        float width = p.measureText(strings);
        maxWidthF = Math.max(width, maxWidthF);
        int maxWidth = (int) (maxWidthF + 0.5);
        if (maxWidth < 0) {
            maxWidth = 0;
        }
        return maxWidth;
    }

    /**
     * 获取绘制文字的高度
     *
     * @param mPaint
     * @return
     */
    public double getTextHeight(Paint mPaint) {
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        return Math.ceil(fm.descent - fm.ascent);
    }

    private int dip2px(float dip) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }

}